<template>
	<view class="u-page">
		<view class="formbox">
			<view :class="rules.realname.error?'item error':'item'">
				<view class="title">姓名：</view>
				<view class="value">
					<u-input placeholder="请输入管理员姓名" v-model="formdata.realname" inputAlign="right" @blur="$validate('realname')"/>
					<view class="tag-danger" v-if="rules.realname.error">{{rules.realname.error}}</view>
				</view>
			</view>	
			<view :class="rules.mobile.error?'item error':'item'">
				<view class="title">手机号：</view>
				<view class="value">
					<u-input placeholder="请输入手机号" v-model="formdata.mobile" inputAlign="right" @blur="$validate('mobile')"/>
					<view class="tag-danger" v-if="rules.mobile.error">{{rules.mobile.error}}</view>
				</view>
			</view>
			<view class="item">
				<view class="title">状态：</view>
				<view class="value">
					<u-radio-group v-model="formdata.status">
					    <u-radio label="正常" name="normal"></u-radio>
						<u-radio label="禁用" name="hidden"></u-radio>
					</u-radio-group>
				</view>
			</view>	
			<view class="item">
				<view class="title">绑定微信：</view>
				<view class="value picker" @click="$navigateTo" data-url="common/wxuser">
					<image v-if="wxuser" :src="wxuser.avatar" style="width: 60rpx;height: 60rpx;margin-right: 10rpx;"/>
					<text v-if="wxuser">{{wxuser.nickname}}</text>
					<text v-else>未选择</text>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="submit">
				<u-button type="primary" text="提交" @click="submit"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {baseUrl,methods} from '@/utils/methods.js';
	import {rand} from '@/utils/util.js';
	export default {
		data() {
			return {
				sizeType:['compressed'],
				rules:{
					realname:{
						title:'姓名',
						rule:'require;chinaChar;length(2~10)',
						error:''
					},
					mobile:{
						title:'手机号',
						rule:'require;mobile',
						error:''
					}
				},
				formdata:{
					id:'',
					mobile:'',
					realname:'',
					status:'normal'
				},
				wxuser:''
			}
		},
		onShow() {
			let wxuser=uni.getStorageSync('check_user') || '';
			if(wxuser){
				this.wxuser=wxuser;
				if(wxuser.mobile && !this.formdata.mobile){
					this.formdata.mobile=wxuser.mobile;
				}
				uni.setStorageSync('check_user','');
			}
		},
		onLoad(e) {
			let seller=uni.getStorageSync('seller') || '';
			if(seller){
				this.formdata={
					id:seller.id,
					mobile:seller.mobile,
					realname:seller.realname,
					status:seller.status+''
				};
				this.wxuser=seller.user;
				uni.setStorageSync('seller','');
				uni.setNavigationBarTitle({
					title:'修改销售经理'
				});
			}
		},
		methods: {
			...methods,
			submit:function(){				
				for(let rule in this.rules){
					if(!this.$validate(rule)){
						return;
					}
				}
				if(this.wxuser){
					this.formdata.user_id=this.wxuser.id;
				}
				this.$post('admin/channel/addSeller',this.formdata,true).then(res=>{
					uni.navigateBack();
				});
			}
		}
	}
</script>

<style lang="scss">
	@import "../common/form.scss"; 
	.u-page{
		padding-bottom: 100rpx;
	}
</style>
